package com.zhanghao.sdkdemo;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.zhanghao.sdk.view.ZHFloatView;
import com.zhanghao.sdk.UnionSDK;
import com.zhanghao.sdk.callback.IZHSDKCallback;
import com.zhanghao.sdk.constant.SERVER;
import com.zhanghao.sdk.ui.UiManager;
import com.zhanghao.sdk.util.IAddZHViewCallback;
import com.zhanghao.sdk.util.IDebugCallback;
import com.zhanghao.sdk.util.ScreenUtils;
import com.zhanghao.sdk.util.ZHLog;
import com.zhanghao.sdk.util.ZHUiUtils;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends Activity {
    private static final String TAG = MainActivity.class.toString();
    private ViewGroup mContainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ZHUiUtils.hideStatusBar(MainActivity.this);
        setContentView(R.layout.activity_main);
        mContainer = findViewById(R.id.container);
        final EditText editText = findViewById(R.id.editText4);
        editText.setKeyListener(null);
        // demo相关的实现 开始
        ZHLog.setDebugCallback(new IDebugCallback() {
            @Override
            public void log(final String debugType, final String debugText) {
                editText.post(new Runnable() {
                    @Override
                    public void run() {

                        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
                        Date date = new Date(System.currentTimeMillis());
                        editText.setText(editText.getText()+ formatter.format(date) + "\t\t" + debugType + ": " + debugText + "\n");
                        editText.setSelection(editText.length());
                    }
                });
            }
        });
        ZHLog.i("mock数据");
        ZHLog.i("mock数据1");
        ZHLog.i("mock数据2");
        ZHLog.i("mock数据3");
        ZHLog.i("mock数据4");
        ZHLog.i("测试测试3");
        ZHLog.i("测试测试3");
        ZHLog.i("测试测试3");
        ZHLog.i("测试测试3");
        ZHLog.i("测试测试3");
        ZHLog.i("测试测试3");
        ZHLog.i("测试测试3");
        ZHLog.i("测试测试3");
        ZHLog.i("测试测试3");
        ZHLog.i("测试测试3");
        ZHLog.i("测试测试3");
        ZHLog.i("测试测试3");
        ZHLog.i("测试测试3");
        ZHLog.i("测试测试3");
        ZHLog.i("测试测试3");
        ZHLog.i("测试测试3");

        View imageButton = findViewById(R.id.imageButton);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Configuration mConfiguration = MainActivity.this.getResources().getConfiguration(); //获取设置的配置信息
                int ori = mConfiguration.orientation ; //获取屏幕方向
                // 横屏
                if(ori == mConfiguration.ORIENTATION_LANDSCAPE) {
                    // 旋转为竖屏
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                } else{
                    // 旋转为横屏
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                }
            }
        });
        // demo相关的实现 结束

        initSDK();
        testUtils();
    }

    private void testUtils() {
        ImageView view = new ImageView(this);
        RelativeLayout layout = new RelativeLayout(this);
        layout.addView(view);
        view.setImageDrawable(getDrawable(R.drawable.ic_launcher));
        // 测试添加View
        final ZHFloatView zhView = ZHUiUtils.addZHView(this, layout, new IAddZHViewCallback() {
            @Override
            public FrameLayout.LayoutParams addViewBefore(FrameLayout.LayoutParams layoutParams) {
                layoutParams.gravity = Gravity.CENTER | Gravity.LEFT;
                return layoutParams;
            }
        });
        DisplayMetrics displayMetrics = ScreenUtils.getDisplayMetrics(this);
        zhView.initFloatEvent(displayMetrics.widthPixels, displayMetrics.heightPixels);
        zhView.setItemOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UiManager.getInstance().showLoginView();
            }
        });
    }

    private void initSDK() {
        UnionSDK.getInstance().init(this, new IZHSDKCallback() {
            @Override
            public void initCallBack(int code, JSONObject data) {
                if (code == SERVER.OK) {
                    UnionSDK.getInstance().login();
                } else {
                    Log.i(TAG, "初始化失败：" + data.toString());
                }
            }

            @Override
            public void loginCallBack(int code, JSONObject data) {
                if (code == SERVER.OK) {
                    Log.i(TAG, "登录成功：" + data.toString());
                } else {
                    Log.i(TAG, "登录失败：" + data.toString());
                }
            }

            @Override
            public void logOutCallBack() {
                Log.i(TAG, "SDK退出");
            }

        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

}
